sugarfreeControllers.controller('MessagesController', ['$scope','$log', '$http', '$filter', '$timeout', '$q','HttpService','ngTableParams','Utils','TimeService','DictionarySerivce','PageService',function($scope,$log,$http, $filter, $timeout, $q,httpService,ngTableParams, Utils,timeService,dictionarySerivce,pageService) {
	var SERVICE_USERID_SYSTEM = 5;
	var SERVICE_USERID_BOOKED = 6;
	var SERVICE_USERID_VERIFIED = 7;
	
	var TYPE_MEMBER = 1;
	var TYPE_COACH = 2;
	var TYPE_GYM = 3;
	var TYPE_ADMIN = 4;
	
	$scope.retrieveMessages = function(mType){ retrieveSwitch(mType); };
	function retrieveSwitch(mType){
		var expandParams;
		switch (mType) {
		case 'scan':
			retrieveMessagesScan(SERVICE_USERID_VERIFIED);
			break;
		case 'booked':
			retrieveMessagesBooked(SERVICE_USERID_BOOKED);
			break;
		case 'system':
			retrieveMessagesSystem(SERVICE_USERID_SYSTEM);
			break;

		default:
			break;
		}
	}
	
	/*
	var retrieveMessagesByPage = function(params){
		$log.info("params",params);
		var sorts = '[]'; 	//sort
		var page = params.page();
		var count = params.count();
		var _orderBy = params._orderBy;
		var _status = params._status;
		var beginDay = null;//
		var endDay = null; //
		var _queryParam ="page="+ page +
						 "&limit="+ count +
						 "&orderByCreatedTime=r.createdtime" +
						 "&orderByVerifiedTime=r.verifiedtime" +
						 "&status="+ _status +
						 "&isPage=1";
		
		console.log(_queryParam);
		return $http.get("receipt/query?" + _queryParam);
	};*/
	
	/*
	function getTableParams(getDataFn,expandParams){
			return new ngTableParams({
				page: 1,            // show first page
				count: 10           // count per page
			},{
				counts: [],
				total: 0, // length of data
				getData:function($defer,params){
					Utils.setProcessParclose(true);
					params = angular.extend(params,expandParams);
					getDataFn(params).success(function (data){
						var pageinfo = data;
						params.total(pageinfo.total);
						angular.forEach(pageinfo.list,function(item){
							item.lesson.beginTime = timeService.patten(item.lesson.begin 
							item.createdTime = timeService.patten(item.createdTime);
							item.verifiedTime = timeService.patten(item.verifiedTime);
						});
						$defer.resolve(pageinfo.list);
					});
					Utils.setProcessParclose(false);
				}
			});
	}*/
	
	function retrieveMessagesScan(fromId){
		var options = {
			expandParams:{
				fromId:fromId,
				fromType:TYPE_ADMIN,
				toType:TYPE_GYM
			},
			getDataFn:function(params){
				var fromId = params.fromId;
	  			var fromType = params.fromType;
	  			var toType = params.toType;
	  			var page = params.page();
	  			var count = params.count();
	  			var _url = "message/query?fromId="+ fromId +
	  								 "&fromType=" + fromType + 
	  								 "&toType=" + toType + 
		  					 		 "&page=" + page +
		  					 		 "&limit=" +count;
		  		return httpService.get(_url);
			},
			procResult:function(data){
				angular.forEach(data.list, function(item) {
					item.time = timeService.patten(item.time);
			    });
				return data;
			}
		};
		$scope.tableParamsScan = pageService.getTableParams(options);
		angular.extend($scope.tableParamsScan.$params,{nowdate:new Date()});
		$scope.tableParamsScan.reload();
	}
	
	function retrieveMessagesBooked(fromId){
		var options = {
			expandParams:{
				fromId:fromId,
				fromType:TYPE_ADMIN,
				toType:TYPE_GYM
			},
			getDataFn:function(params){
				var fromId = params.fromId;
	  			var fromType = params.fromType;
	  			var toType = params.toType;
	  			var page = params.page();
	  			var count = params.count();
	  			var _url = "message/query?fromId="+ fromId +
	  								 "&fromType=" + fromType + 
	  								 "&toType=" + toType + 
		  					 		 "&page=" + page +
		  					 		 "&limit=" +count;
		  		return httpService.get(_url);
			},
			procResult:function(data){
				angular.forEach(data.list, function(item) {
					item.time = timeService.patten(item.time);
			    });
				return data;
			}
		};
		$scope.tableParamsBooked = pageService.getTableParams(options);
		angular.extend($scope.tableParamsBooked.$params,{nowdate:new Date()});
		$scope.tableParamsBooked.reload();
	}
	function retrieveMessagesSystem(fromId){
		var options = {
			expandParams:{
				fromId:fromId,
				fromType:TYPE_ADMIN,
				toType:TYPE_GYM
			},
			getDataFn:function(params){
				var fromId = params.fromId;
	  			var fromType = params.fromType;
	  			var toType = params.toType;
	  			var page = params.page();
	  			var count = params.count();
	  			var _url = "message/query?fromId="+ fromId +
	  								 "&fromType=" + fromType + 
	  								 "&toType=" + toType + 
		  					 		 "&page=" + page +
		  					 		 "&limit=" +count;
		  		return httpService.get(_url);
			},
			procResult:function(data){
				angular.forEach(data.list, function(item) {
					item.time = timeService.patten(item.time);
			    });
				return data;
			}
		};
		$scope.tableParamsSystem = pageService.getTableParams(options);
		angular.extend($scope.tableParamsSystem.$params,{nowdate:new Date()});
		$scope.tableParamsSystem.reload();
	}
	
	function initData(){
		
	}
	
	initData();
}]);